package main

import (
	"../fileutil"
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strings"
)

func main() {

	scanner := bufio.NewReader(os.Stdin)

	originalPath, _ := fileutil.GetInputLine(scanner, "请输入原图目录：")
	revisePath, _ := fileutil.GetInputLine(scanner, "请输入原图修正目录：")
	picturePath, _ := fileutil.GetInputLine(scanner, "请输入壁纸目录：")
	outPath, _ := fileutil.GetInputLine(scanner, "请输入输出目录：")

	CleanPicture(originalPath, revisePath, picturePath, outPath)

}

func CleanPicture(originalPath, revisePath, picturePath, outPath string) {
	// 创建输出目录
	picOutPath, _ := fileutil.MkDir(outPath, "壁纸")
	originalOutPath, _ := fileutil.MkDir(outPath, "原图")
	reviseOutPath, _ := fileutil.MkDir(outPath, "原图修正")

	originalSet := make(map[string]bool)
	pictureMap := make(map[string]os.FileInfo)
	compile := regexp.MustCompile("combine(\\d+)")

	originalDeleteCount := 0 // 原图删除数
	combineCount := 0        // 组合图数量
	pictureDeleteCount := 0  // 壁纸删除数
	reviseDeleteCount := 0   // 原图修正删除数
	picReviseCount := 0      // 壁纸中的修正数

	// 待删除切片
	picDeleteList := make([]string, 0)
	reviseDeleteList := make([]string, 0)
	originalDeleteList := make([]string, 0)

	fmt.Println("Start . . . ")

	// 处理壁纸
	pictures, _ := ioutil.ReadDir(picturePath)
	for _, picture := range pictures {
		fileName := picture.Name()

		// 移除系统文件
		if fileutil.DealDesktop(picturePath, fileName) {
			continue
		}

		// 按照 “-” 分割文件名，并去除处理符号集（rotate、tailor、combine等括号内的全部内容）
		a := strings.LastIndexByte(fileName, '.')
		names := strings.Split(fileName[0:a], "-")
		if len(names) < 3 {
			panic("文件名错误： 可能是由于 " + fileName + " 确实‘-’分隔符")
		}
		if strings.Index(names[1], "rotate") > 0 ||
			strings.Index(names[1], "tailor") > 0 ||
			strings.Index(names[1], "combine") > 0 {
			picReviseCount++
		}
		names1Group := compile.FindStringSubmatch(names[1])
		names[1] = names[1][0:strings.IndexByte(names[1], '(')]

		// 移除 【4K】
		if names[2][len(names[2])-8:] == "【4K】" {
			names[2] = names[2][0 : len(names[2])-8]
		} else {
			fmt.Println(names[2] + " 后4位为： " + names[2][len(names[2])-8:])
			panic("壁纸文件 " + fileName + " 缺少‘【4K】’后缀")
		}

		// 将壁纸信息存入map
		newName := names[0] + "-" + names[1] + "-" + names[2]
		if _, ok := pictureMap[newName]; ok == true {
			panic("壁纸 " + fileName + " 已存在！")
		}
		pictureMap[newName] = picture

		// 处理组合图，顺便记个数
		if len(names1Group) > 0 {
			names[1] = names[1][0:strings.LastIndexByte(names[1], 'p')+1] + names1Group[1]
			newName = names[0] + "-" + names[1] + "-" + names[2]
			if _, ok := pictureMap[newName]; ok == true {
				panic("壁纸 " + fileName + " 已存在！")
			}
			pictureMap[newName] = picture
			combineCount++
		}
	}

	// 清除原图
	originalPictures, _ := ioutil.ReadDir(originalPath)
	for _, picture := range originalPictures {
		fileName := picture.Name()
		if fileutil.DealDesktop(originalPath, fileName) {
			continue
		}

		a := strings.LastIndexByte(fileName, '.')
		name := fileName[0:a]
		if _, ok := pictureMap[name]; ok {
			originalSet[name] = true
			delete(pictureMap, name)
		} else {
			// 如果原图没有对应的壁纸，则删除
			originalDeleteList = append(originalDeleteList, fileName)
			originalDeleteCount++
		}
	}

	// 清除壁纸
	for name, picture := range pictureMap {
		if _, ok := originalSet[name]; ok == false {
			// 如果壁纸没有对应的原图，则删除
			picDeleteList = append(picDeleteList, picture.Name())
			pictureDeleteCount++
		}
	}

	// 清除原图修正
	revisePictures, _ := ioutil.ReadDir(revisePath)
	for _, picture := range revisePictures {
		fileName := picture.Name()
		if fileutil.DealDesktop(revisePath, fileName) {
			continue
		}

		// 处理原图修正文件名
		a := strings.LastIndexByte(fileName, '.')
		names := strings.Split(fileName[0:a], "-")
		if len(names) < 3 {
			panic(" 原图修正图文件名 " + fileName + " 有问题！可能是无括号或者缺少“-”分隔符")
		}
		name := names[0] + "-" + names[1][0:strings.LastIndexByte(names[1], '(')] + "-" + names[2]

		// 如果没有对应的原图，则删除原图修正
		if _, ok := originalSet[name]; !ok {
			reviseDeleteList = append(reviseDeleteList, fileName)
			reviseDeleteCount++
		}
	}

	// 统一删除
	fileutil.MoveFiles(picDeleteList, picturePath, picOutPath)
	fmt.Println()
	fileutil.MoveFiles(originalDeleteList, originalPath, originalOutPath)
	fmt.Println()
	fileutil.MoveFiles(reviseDeleteList, revisePath, reviseOutPath)
	fmt.Println()

	fmt.Println("---------------------------------------------------")
	fmt.Printf("原图删除数：%d\n", originalDeleteCount)
	fmt.Printf("壁纸删除数：%d\n", pictureDeleteCount)
	fmt.Printf("原图修正删除数：%d\n", reviseDeleteCount)
	fmt.Printf("原图剩余数：%d 和combine数量：%d\n", len(originalPictures)-reviseDeleteCount-combineCount, combineCount)
	fmt.Printf("壁纸剩余数：%d 包含修正数：%d\n", len(pictures)-pictureDeleteCount, picReviseCount)
	fmt.Println("END!")
}
